package mm.chenchen.modules.script.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mm.chenchen.modules.script.dao.model.request.ScriptMirroringQuery;
import mm.chenchen.modules.script.entity.ScriptMirroring;
import mm.chenchen.modules.script.service.ScriptMirroringService;
import mm.chenchen.utils.PageUtils;
import mm.chenchen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author
 * @email
 * @date 2021-07-29 10:50:49
 */
@RestController
@Api(tags = "镜像管理")
@RequestMapping("reptile/scriptmirroring")
@CrossOrigin
public class ScriptMirroringController {
    @Autowired
    private ScriptMirroringService service;


    /**
     * 列表
     */
    @ApiOperation(value = "分页检索", notes = "")
    @PostMapping("/list")
    //@RequiresPermissions("reptile:scriptpythonmirroring:list")
    public R findPage(@RequestBody ScriptMirroringQuery param) {
        return service.findPage(PageUtils.getPage(param), param);
    }

    @ApiOperation(value = "下拉选择", notes = "")
    @PostMapping("drop/down/list")
    //@RequiresPermissions("reptile:scriptpythonmirroring:list")
    public R dropDownList(@RequestBody ScriptMirroringQuery param) {
        return service.dropDownList(param);
    }

    /**
     * 信息
     */
    @ApiOperation(value = "单个检索 ", notes = "")
    @GetMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:scriptpythonmirroring:info")
    public R getById(@ApiParam(name = "objId", value = "") @PathVariable Long objId) {
        return service.getById(objId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增", notes = "")
    @PostMapping(value = "")
    //@RequiresPermissions("reptile:scriptpythonmirroring:save")
    public R save(@RequestParam Map<String, Object> map, @RequestParam("file") MultipartFile file) throws Exception {
        System.out.println(map);
        ScriptMirroring data = new ScriptMirroring();
        data.setName((String) map.get("name"));
        data.setVersion((String) map.get("version"));
        if (ObjectUtils.isEmpty(file)) {
            return R.error("未传入文件");
        }

        return service.saveInfo(data,file);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新", notes = "")
    @PutMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:scriptpythonmirroring:update")
    public R update(@ApiParam(name = "objId", value = "") @PathVariable Long objId, @RequestBody ScriptMirroring param) {
        param.setId(objId);
        return service.update(param);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:scriptpythonmirroring:delete")
    public R deleteById(@ApiParam(name = "objId", value = "") @PathVariable Long objId) {


        return service.deleteById(objId);
    }

}
